import java.nio.ByteBuffer;
import java.util.*;
import com.google.gson.Gson;
import com.google.gson.JsonObject;

public class HumiditySensor extends VirtualSensor {

	private HashMap<String, String> bodyCopies;

	public HumiditySensor(VSensorConfig config, SocketSender sender) {
		super(config, sender);
		bodyCopies = new HashMap<String, String>(100);
	}

	protected String prepareJsonStr() {

		String str = TemperatureData.getCurrentDateTime("2012-03-05", 30);
		Integer value = TemperatureData.getCurrentHumidity(str);
		bodyCopies.put("SensorId", String.valueOf(config.getSensorID()));
		bodyCopies.put("Value", String.valueOf(value));
		bodyCopies.put("DcTime", str);
		Gson gson = new Gson();
		JsonObject jsonobject = new JsonObject();
		for (String strKey : bodyCopies.keySet())
			jsonobject.addProperty(strKey, bodyCopies.get(strKey));
		String jsonStr = gson.toJson(jsonobject);
		bodyCopies.clear();
		return jsonStr;
	}

	@Override
	public void run() {
		try {

			String path = System.getProperty("user.dir");
			path = path + "/weatherdata.txt";
			TemperatureData.fillHumidityData(path);
			while (!this.isInterrupted()) {
				if (sender.isAlive()) {
					List<ByteBuffer> bufferList = sender.getBufferList();
					synchronized (bufferList) {
						ByteBuffer bb = prepareData();
						bufferList.add(bb);
						bufferList.notify();
					}
					System.out.println("Config.getGenerateRate = "
							+ config.getGenerate_rate());
				} else
					break;
				Thread.sleep(config.getGenerate_rate());
			}
		} catch (InterruptedException e) {
			System.out.println(Thread.currentThread().getName()
					+ " interrupted");
			// e.printStackTrace();
		}
	}

}
